<?php
/**
 * 用户中心-内容管理
 * @author zengnz
 *
 */

class ContentAction extends CommonAction
{
	/**
	 * (non-PHPdoc)
	 * @see CommonAction::_initialize()
	 */
	public function _initialize() {
		$userAuthKey = session(C('USER_AUTH_KEY'));
		$account = session('customer');
		if ( !isset($userAuthKey) || !$userAuthKey || !$account )
			redirect ( PHP_FILE . C ( 'USER_AUTH_GATEWAY' ) );
	}
	
	//首页
	public function index() {
		
		$this -> assign('action', 'content');
		$this -> display();
	}
	
	/**
	 * 消息中心
	 */
	public function message() {
		$uId = $this -> getUid();
		
		$MessageLogic = D('Message', 'Logic');
		$sendCount = $MessageLogic -> getMsgCount($uId, 1);
		$getCount = $MessageLogic -> getMsgCount($uId, 2);
		$sysCount = $MessageLogic -> getMsgCount($uId, 3);
		
		$this -> assign('sendCount', $sendCount);
		$this -> assign('getCount', $getCount);
		$this -> assign('sysCount', $sysCount);
		
		$this -> display();
	}
	
	/**
	 * ajax获取消息列表
	 */
	public function messageList() {
		$uId = $this -> getUid();
		$type = $this -> getParam('type');
		$page = $this -> getParam('page');
		$page = $page ? $page : 1;
		
		$MessageLogic = D('Message', 'Logic');
		import ( "@.ORG.Page" );
		
		$count = $MessageLogic -> getMsgCount($uId, $type);
		$PageModel = new Page($count, $page);
		$pageList = $PageModel -> ajaxList();
		
		$list = $MessageLogic -> getMegList($uId, $type, $PageModel->firstRow, $PageModel->listRows);
		
		$msgList = array();
		if (is_array($list) && !empty($list)) {
			foreach ($list as $key => $val) {
				$msgList[$key] = $val;
				$msgList[$key]['create_time'] = date('Y-m-d H:i:s', $val['create_time']);
			}
		}
		
		$this -> assign('pageList', $pageList);
		$this -> assign('msgList', $msgList);
		$this -> assign('type', $type);
		$this -> assign('curPage', $page);
		$this -> display();
	}
	
	/**
	 * 发送消息
	 */
	public function sendMsg() {
		if ($this -> isPost()) {
			$params = $_POST;
			$from = $this -> getUid();
			$to = $params['t_id'];
			$msg = $params['content'];
			
			$ret = D('Message', 'Logic') -> sendMsg($from, $to, $msg);
			if ($ret == 1)
				$this -> redirect('message');
		}
		
		$this -> display();
	}
}
